package uk.co.richardwkirk.parkrun.android;

import uk.co.richardwkirk.parkrun.android.events.displays.ClosestEvent;
import uk.co.richardwkirk.parkrun.android.events.displays.EventSelection;
import uk.co.richardwkirk.parkrun.android.news.NewsDisplay;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends SharedDataReadyActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// Select a country but do not do anything further
		useCountry(IntentExtras.REQUEST_COUNTRY_SELECT_ONLY);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main, menu);
	    return true;
	}
	
	
	/***
	 *** CLICK HANDLERS
	 ***/
	
	/**
	 * Start the athlete choice activity.
	 * @param view
	 */
	public void showAthleteChoice(View view) {
		Intent intent = new Intent(this, AthleteChoice.class);
		intent.putExtra(IntentExtras.EXTRA_ATHLETE_CHOICE_MODE, IntentExtras.ATHLETE_CHOICE_MODE_DISPLAY);
		startActivity(intent);	
	}
	

	/**
	 * Start the event finder activity.
	 * @param view
	 */
	public void showEventFinder(View view) {
		loadEventData(IntentExtras.REQUEST_EVENT_DATA_LOAD_ALL_EVENTS);
	}
	
	
	/**
	 * Start the closest event finder activity.
	 * @param view
	 */
	public void showClosestEventFinder(View view) {
		loadEventData(IntentExtras.REQUEST_EVENT_DATA_LOAD_CLOSEST_EVENT);
	}
	
	/**
	 * Start the closest event finder activity.
	 * @param view
	 */
	public void showCountryNews(View view) {
		loadEventData(IntentExtras.REQUEST_EVENT_DATA_LOAD_NEWS);
	}
	
	/**
	 * Change the selected country.
	 * @param view
	 */
	public void selectCountry(View view) {
		loadEventData(IntentExtras.REQUEST_EVENT_DATA_LOAD_COUNTRY_SELECTION);
	}	
	
	
	/***
	 *** REQUEST HANDLERS
	 ***/

	

	/**
	 * Start the relevant display following an event data load.
	 * @param requestCode
	 */
	protected void handleEventLoadResponse(int requestCode) {
		switch (requestCode) {
			case IntentExtras.REQUEST_EVENT_DATA_LOAD_NEWS:
				useCountry(IntentExtras.REQUEST_NEWS_COUNTRY_SELECT);
				break;
			case IntentExtras.REQUEST_EVENT_DATA_LOAD_CLOSEST_EVENT:
				Intent intent = new Intent(this, ClosestEvent.class);
				startActivity(intent);
				break;
			case IntentExtras.REQUEST_EVENT_DATA_LOAD_ALL_EVENTS:
				useCountry(IntentExtras.REQUEST_EVENT_COUNTRY_SELECT);
				break;
			case IntentExtras.REQUEST_EVENT_DATA_LOAD_COUNTRY_SELECTION:
				changeDefaultCountry(IntentExtras.REQUEST_CHANGE_DEFAULT_COUNTRY);
				break;
		}
	}
	
	/**
	 * Process the result of a country selection and decide which activity to display next.
	 */
	protected void handleCountrySelectionResult(int requestCode, Intent data)	{
		String country = data.getStringExtra(IntentExtras.EXTRA_SELECTED_COUNTRY);
		
	    // Set the picture for the country
	    setCountryImage(country);
		showCountryDisplay(requestCode, country);
	}
	
	private void setCountryImage(String country) {
		// Country could be null if not yet loaded
		ImageView countryImage = (ImageView)findViewById(R.id.country_icon);
		if ("Australia".equals(country)) {
			countryImage.setImageResource(R.drawable.au);
		}
		else if ("UK".equals(country)) {
			countryImage.setImageResource(R.drawable.uk);
		}
		else if ("Denmark".equals(country)) {
			countryImage.setImageResource(R.drawable.dk);
		}
		else if ("USA".equals(country)) {
			countryImage.setImageResource(R.drawable.us);
		}
		else if ("Iceland".equals(country)) {
			countryImage.setImageResource(R.drawable.is);
		}
		else if ("New Zealand".equals(country)) {
			countryImage.setImageResource(R.drawable.nz);
		}
		else if ("Ireland".equals(country)) {
			countryImage.setImageResource(R.drawable.ie);
		}
		else if ("South Africa".equals(country)) {
			countryImage.setImageResource(R.drawable.za);
		}
		else if ("Poland".equals(country)) {
			countryImage.setImageResource(R.drawable.pl);
		}
		else if ("Russia".equals(country)) {
			countryImage.setImageResource(R.drawable.ru);
		}
		else if ("Singapore".equals(country)) {
			countryImage.setImageResource(R.drawable.sg);
		}
		else {
			Log.e("PARKRUN", "Missing icon for country: " + country);
			countryImage.setImageResource(R.drawable.globe);
		}
	}

	/** 
	 * Start the relevant display following a country selection
	 */
	public void showCountryDisplay(int requestCode, String country) {
		switch (requestCode) {
			case IntentExtras.REQUEST_NEWS_COUNTRY_SELECT:
				showCountryNewsActivity(country);
				break;
			case IntentExtras.REQUEST_EVENT_COUNTRY_SELECT:
				showEventDisplayActivity(country);
				break;
		}
	}
	
	private void showEventDisplayActivity(String country) {
		Intent intent = new Intent(this, EventSelection.class);
		intent.putExtra(IntentExtras.EXTRA_SELECTED_COUNTRY, country);
		startActivity(intent);
	}

	private void showCountryNewsActivity(String country) {
		Intent intent = new Intent(this, NewsDisplay.class);
		intent.putExtra(IntentExtras.EXTRA_NEWS_TYPE, IntentExtras.EXTRA_NEWS_TYPE_COUNTRY);
		intent.putExtra(IntentExtras.EXTRA_SELECTED_COUNTRY, country);
		startActivity(intent);
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.default_country:
				loadEventData(IntentExtras.REQUEST_EVENT_DATA_LOAD_COUNTRY_SELECTION);
				break;
			default:
				return super.onOptionsItemSelected(item);
		}
		return true;
	}

	@Override
	protected void handleAthleteLoadResponse(int requestCode) {
		// Nothing required. We don't load athletes here.
	}

	@Override
	protected void handleDataLoadError(int requestCode) {
		// Nothing required. Error messages will have already been shown.
	}

	@Override
	protected void handleDataLoadCancel(int requestCode) {
		// Nothing required. Error messages will have already been shown.
	}
}
